package com.sz.controller;


import com.sz.model.BankResult;
import com.sz.model.Result;
import com.sz.myinterface.SMSCode;
import com.sz.service.IFrozenService;
import com.sz.utils.Md5Util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.Action;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/frozen")
public class FrozenController {

    @Autowired
    private Md5Util util;

    @Autowired
    private IFrozenService iFrozenService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // 进行用户银行卡冻结
    @RequestMapping("/verify") // 前端传银行卡卡号，身份证号，银行密码，验证码
    public Result UserFrozen(String bankAccount, String ukNumber, String bankPwd, String checkCode) {
        System.out.println(bankAccount+"--------------");
        BankResult result = iFrozenService.getByAccount(bankAccount);
        System.out.println("result.toString() = " + result.toString());
        System.out.println("result.getUkNumber() = " + result.getUkNumber());
        System.out.println("result.getBankPwd() = " + result.getBankPwd());
        //if (result.getUkNumber().equals(ukNumber) && util.passwordEqualMD5(bankPwd, result.getBankPwd())){
        if (result!=null&&result.getUkNumber().equals(ukNumber) && result.getBankPwd().equals(bankPwd)){
            String sc = stringRedisTemplate.opsForValue().get("checkCode");
            //System.out.println("sc = " + sc);
            System.out.println(sc+"------"+checkCode);
            if (sc!=null&&sc.equals(checkCode)) {

                // 改变银行卡状态
                iFrozenService.changeState(bankAccount);
                return new Result(200,"冻结成功");
            }else {
                return new Result(305,"冻结失败");
            }
        }

        return new Result(306,"冻结失败");
    }

    @RequestMapping("/sendNew") // 发送短信验证，并将验证码存到redis
    public String sendNews(String mobile) {
        String sc = SMSCode.sendSMS(mobile);
        stringRedisTemplate.opsForValue().set("checkCode", sc,1, TimeUnit.MINUTES);
        return sc;
    }
}